namespace KinonekoSoftware.Studio.Pages.Theories
{
    public sealed class NewKeywordCatalogViewModel : ObjectDialog<KeywordCatalog>
    {

        private string         _text;
        private bool           _useCustomType;
        private string         _customType;
        private InstanceType   _type;
        private KeywordCatalog _parent;
        private KeywordCatalog _target;


        public static Task<Result<KeywordCatalog>> New()
        {
            var context = new DialogContext();
            
            context.Set(ContextBase.Key_Parameter2, false);
            
            return Dialog<KeywordCatalog, NewKeywordCatalogViewModel>(context);
        }
        
        public static Task<Result<KeywordCatalog>> New(KeywordCatalog parent)
        {
            var context = new DialogContext();
            
            context.Set(ContextBase.Key_Parameter, parent);
            context.Set(ContextBase.Key_Parameter2, false);
            
            return Dialog<KeywordCatalog, NewKeywordCatalogViewModel>(context);
        }
        
        public static Task<Result<KeywordCatalog>> Edit(KeywordCatalog target)
        {
            var context = new DialogContext();
            
            context.Set(ContextBase.Key_Parameter, target);
            context.Set(ContextBase.Key_Parameter2, true);
            
            return Dialog<KeywordCatalog, NewKeywordCatalogViewModel>(context);
        }

        public NewKeywordCatalogViewModel()
        {
            Title       = TitleSR.New_Folder;
            Description = TextSR.Data_Name;
        }
        
        protected override bool OnReceive(DialogContext parameter)
        {
            var r = parameter.Has(ContextBase.Key_Parameter2) && parameter.GetBoolean(ContextBase.Key_Parameter2);
            
            if (r)
            {
                _target = parameter.GetObject<KeywordCatalog>(ContextBase.Key_Parameter);
            }
            else if(parameter.Has(ContextBase.Key_Parameter))
            {
                
                _parent = parameter.GetObject<KeywordCatalog>(ContextBase.Key_Parameter);
            }
            
            return r;
        }

        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(
                      DialogRule.Create<NewKeywordCatalogViewModel>(TextSR.Parameter_Empty_Value,
                                                                                x => !string.IsNullOrEmpty(x.Text)));
        }

        protected override KeywordCatalog OnFinish(bool editing)
        {
            if (editing)
            {
                _target.Name          = Text;
                _target.CustomType    = CustomType;
                _target.UseCustomType = UseCustomType;
                return _target;
            }


            if (_useCustomType)
            {
                return new KeywordCatalog
                {
                    Id            = ID.Get(),
                    ParentID      = _parent?.Id,
                    Name          = _text,
                    CustomType    = _customType,
                    UseCustomType = true,
                };
            }
            
            
            return new KeywordCatalog
            {
                Id            = ID.Get(),
                ParentID      = _parent?.Id,
                Name          = _text,
                Type          = _type,
                UseCustomType = false,
            };
        }

        /// <summary>
        /// 获取或设置 <see cref="CustomType"/> 属性。
        /// </summary>
        public string CustomType
        {
            get => _customType;
            set => SetValue(ref _customType, value);
        }
        
        
        /// <summary>
        /// 获取或设置 <see cref="Type"/> 属性。
        /// </summary>
        public InstanceType Type
        {
            get => _type;
            set => SetValue(ref _type, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="UseCustomType"/> 属性。
        /// </summary>
        public bool UseCustomType
        {
            get => _useCustomType;
            set => SetValue(ref _useCustomType, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="Text"/> 属性。
        /// </summary>
        public string Text
        {
            get => _text;
            set => TryFinishAndSetValue(ref _text, value);
        }

        /// <summary>
        /// 
        /// </summary>
        public IEnumerable<InstanceType> InstanceTypes => EnumValuesFactory<InstanceType>.Values;
    }
}